package com.byd.base;

import java.util.LinkedList;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.byd.utils.Lg;

public abstract class ParentFragment extends BaseFragment {
	/** 注意：由于几个不同的{@link ParentFragment}用的是同一个布局， 这就导致它们的Container拥有相同的Id， 在调用
	 * {@link FragmentTransaction#add(int, Fragment)}时，
	 * 可能会将这几个不同Parent的Fragment添加进同一个Container！ <br/>
	 * <br/>
	 * 因此，我们需要将这几个Id相同的Container重新修改Id，使它们的Id不同。 */
	private static int ID = 0;
	private LinkedList<ChildFragment> mChildFragments;
	private ChildFragment mMainFragment;
	private Fragment mCurrentFragment;
	private int mChildContainerId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mChildFragments = new LinkedList<ChildFragment>();
		regiestUserAction();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View view = super.onCreateView(inflater, container, savedInstanceState);
		mMainFragment = onMainFragment();
		mMainFragment.setParentFragment(this);
		final View conView = view.findViewById(onChildContainerId());
		conView.setId(mChildContainerId = 1000 + ID++);
		addFragment(mMainFragment);
		return view;
	}

	/** 返回主页Fragment */
	protected abstract ChildFragment onMainFragment();

	/** 返回子Fragment布局文件ID */
	protected abstract int onChildContainerId();

	public void addChildFragment(Class<? extends ChildFragment> cls) {
		try {
			final ChildFragment fragment = cls.newInstance();
			addChildFragment(fragment);
		}
		catch (final Exception e) {
			Lg.printStackTrace(e);
		}
	}

	public void addChildFragment(ChildFragment fragment) {
		if(fragment != null) {
			if(mChildFragments.size() == 0) {
				hideFragment(mMainFragment);
			}
			else {
				hideFragment(mChildFragments.getLast());
			}
			mChildFragments.add(fragment);
			fragment.setParentFragment(this);
			addFragment(fragment);
		}
	}

	public boolean removeLastChildFragment() {
		if(mChildFragments.size() > 0) {
			final Fragment fragment = mChildFragments.removeLast();
			if(mChildFragments.size() <= 0) {
				showFragment(mMainFragment);
			}
			else {
				showFragment(mChildFragments.getLast());
			}
			removeFragment(fragment);
			return true;
		}
		return false;
	}

	private void showFragment(Fragment fragment) {
		final FragmentManager manager = getFragmentManager();
		final FragmentTransaction transaction = manager.beginTransaction();
		transaction.show(fragment);
		transaction.commit();
		mCurrentFragment = fragment;
	}

	private void hideFragment(Fragment fragment) {
		final FragmentManager manager = getFragmentManager();
		final FragmentTransaction transaction = manager.beginTransaction();
		transaction.hide(fragment);
		transaction.commit();
	}

	private void addFragment(Fragment fragment) {
		final FragmentManager manager = getFragmentManager();
		final FragmentTransaction transaction = manager.beginTransaction();
		transaction.add(mChildContainerId, fragment, fragment.getClass().getSimpleName());
		transaction.commit();
		mCurrentFragment = fragment;
	}

	private void removeFragment(Fragment fragment) {
		final FragmentManager manager = getFragmentManager();
		final FragmentTransaction transaction = manager.beginTransaction();
		transaction.remove(fragment);
		transaction.commit();
	}

	@Override
	public boolean onBackPressed() {
		return removeLastChildFragment();
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(mCurrentFragment != null) {
			mCurrentFragment.onHiddenChanged(hidden);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mChildFragments.clear();
	}
}
